<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('养修服务单项')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="serviceItem-form">
                <div class="select-list">
                    <ul>
                        <li>
                            名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            是否套餐：<select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li>
                            服务分类：<select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li>
                            审核状态：<select name="auditStatus" th:with="type=${@dict.getType('si_audit_status')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li>
                            上架状态：<select name="saleStatus" th:with="type=${@dict.getType('si_sale_status')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:serviceItem:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-warning auditBtn" id="startAuditBtn" >
                <i class="fa fa-download"></i> 发起审核
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('business:serviceItem:edit')}]];
    var removeFlag = [[${@permission.hasPermi('business:serviceItem:remove')}]];
    var carPackageDatas = [[${@dict.getType('si_car_package')}]];
    var serviceCatalogDatas = [[${@dict.getType('si_service_catalog')}]];
    var auditStatusDatas = [[${@dict.getType('si_audit_status')}]];
    var saleStatusDatas = [[${@dict.getType('si_sale_status')}]];
    var prefix = ctx + "business/serviceItem";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/delete",
            exportUrl: prefix + "/export",
            sortName: "seq",
            modalName: "养修服务项",
            columns: [{
                checkbox: true
            },
                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },

                {
                    field: 'originalPrice',
                    title: '服务项原价',
                    sortable: true
                },
                {
                    field: 'discountPrice',
                    title: '服务项折扣价',
                    sortable: true
                },
                {
                    field: 'carPackage',
                    title: '是否套餐',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(carPackageDatas, value);
                    },
                    sortable: true
                },
                {
                    field: 'serviceCatalog',
                    title: '服务分类',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(serviceCatalogDatas, value);
                    },
                    sortable: true
                },
                {
                    field: 'auditStatus',
                    title: '审核状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(auditStatusDatas, value);
                    },
                    sortable: true
                },
                {
                    field: 'saleStatus',
                    title: '上架状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(saleStatusDatas, value);
                    },
                    sortable: true
                },
                {
                    field: 'info',
                    title: '备注信息',
                    sortable: true,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 5, "open");
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var editBtnFlag = editFlag;
                        if (row.saleStatus || row.auditStatus == 1) {
                            // 编辑按钮: 1.上架变灰   2. 审核中变灰
                            editBtnFlag += 'disabled';
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + editBtnFlag + '" href="javascript:void(0)" ' +
                            'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        if (row.saleStatus) {
                            // 状态为上架的时候才显示
                            actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)"' +
                                ' onclick="saleOffOp(\'' + row.id + '\')"><i class="fa fa-arrow-down"></i> 下架</a>');
                        } else {
                            // 上架按钮: 状态为下架的时候才显示 非套餐,直接上架 是套餐: 处于非审核通过变灰
                            var saleFlag = "";
                            if (row.carPackage && row.auditStatus != 2) {
                                saleFlag = "disabled";
                            }
                            actions.push('<a class="btn btn-info btn-xs ' + saleFlag + '" href="javascript:void(0)"' +
                                ' onclick="saleOnOp(\'' + row.id + '\')"><i class="fa fa-arrow-up"></i> 上架</a>');
                        }
                        var remove = "";
                        if(row.saleStatus || row.auditStatus == 1){
                            // 1.上架变灰   2. 审核中变灰
                            remove = "disabled";
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + remove + '" href="javascript:void(0)"' +
                            ' onclick="deleteBtn(\'' + row.id + '\')" style="margin-left:3px;"><i class="fa fa-remove"></i> 删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options); // 用户初始化整个table列表
        $("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
            // 判断只有套餐的情况,按钮变亮
            var rows = $('#bootstrap-table').bootstrapTable('getSelections');
            if (rows.length != 1) {
                $("#startAuditBtn").addClass("disabled");
                return;
            }
            // 只有一条的情况下,这条记录是否是套餐
            var row = rows[0];
            // 如果不是套餐,把按钮变灰
            if(!row.carPackage){
                $("#startAuditBtn").addClass("disabled");
                return;
            }
            // 处于审核中或者处于审核通过
            if(row.auditStatus==1 || row.auditStatus==2){
                $("#startAuditBtn").addClass("disabled");
                return;
            }
            $("#startAuditBtn").removeClass("disabled");
        });
        $("#startAuditBtn").on("click",function () {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            $.modal.open("发起审核", prefix + "/auditPage?id="+row.id);
        });
    });
    function saleOffOp(id) {
        $.modal.confirm("是否需要下架该商品?",function () {
            $.operate.get(prefix+"/saleOff?id="+id);
        })
    }
    function saleOnOp(id) {
        $.modal.confirm("是否需要上架该商品?",function () {
            $.operate.get(prefix+"/saleOn?id="+id);
        })
    }
    function deleteBtn(id) {
        $.modal.confirm("确定删除该商品?",function () {
            $.operate.get(prefix+"/delete?id="+id);
        })
    }

</script>
</body>
</html>